package org.blogwu.learn.arithmetic.demo2;

import java.util.Arrays;

/**
 * 选择排序
 */
public class SelectionSort {
    public static void main(String[] args) {
        int[] old = {6, 12, 5, 8, 21, 3, 9, -1};
        // {-1, 12,5 , 8, 21, 3,9,6};
        // {-1, 12,5 , 8, 21, 3,9,6};
        for (int i = 0; i < old.length - 1; i++) {
            int min = old[i];
            // 最小索引
            int minIndex = i; // 1
            for (int j = i + 1; j < old.length; j++) {
                if (old[j] < min) {  //5   3
                    min = old[j]; // 5  3
                    minIndex = j; // 2  5
                }
            }
            if (minIndex != i) {
                int temp = old[i];
                old[i] = old[minIndex];
                old[minIndex] = temp;
            }
        }
        System.out.println("S:" + Arrays.toString(old));
    }
}
